package io.scnb.lesson001.demo11;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LazyBeanTest {

    @Test
    public void actualTimeBean() {
        System.out.println("spring容器启动中...");
        String beanXml = "classpath:/io/scnb/lesson001/demo11/actualTimeBean.xml";
        new ClassPathXmlApplicationContext(beanXml);
        System.out.println("spring容器启动完毕...");
    }

    @Test
    public void lazyInitBean() {
        System.out.println("spring容器启动中...");
        String beanXml = "classpath:/io/scnb/lesson001/demo11/lazyInitBean.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(beanXml);
        System.out.println("spring容器启动完毕...");
        System.out.println("从容器中开始查找LazyInitBean");
        LazyInitBean lazyInitBean = context.getBean(LazyInitBean.class);
        System.out.println("LazyInitBean:" + lazyInitBean);
    }

    /**
     * 从输出结果可以看出，延迟创建的bean也在spring容器启动过程中创建好了
     * 因为在创建实时创建的bean actualTimeDependencyLazyBean时，需要注入LazyInitBean，
     * 所以会去spring容器中查找该lazyInitBean，然后会进行初始化
     */
    @Test
    public void actualTimeDependencyLazyBean() {
        System.out.println("spring容器启动中...");
        String beanXml = "classpath:/io/scnb/lesson001/demo11/actualTimeDependencyLazyBean.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(beanXml);
        System.out.println("spring容器启动完毕...");
    }
}
